package com.escape.oss.util;

import com.escape.oss.entity.FileInfo;
import io.minio.*;
import io.minio.http.Method;
import io.minio.messages.Bucket;
import io.minio.messages.Item;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: escape_zebra
 * @CreateTime: 2025-01-02  17:04
 * @Description: minio 文件操作工具
 * @Version: 1.0
 */
@Component
public class MinioUtil {

    @Resource
    private MinioClient minioClient;

    /**
     * 创建 bucket 桶
     * @param bucketName
     */
    public void createBucket(String bucketName) throws Exception {
        // 1. 判断该桶是否已存在
        boolean exists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
        if (!exists) {
            // 2. 不存在创建桶
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
        }
    }

    /**
     * 上传文件
     * @param inputStream
     * @param bucket
     * @param objectName
     * @throws Exception
     */
    public void uploadFile(InputStream inputStream, String bucket, String objectName) throws Exception {
        minioClient.putObject(
                PutObjectArgs
                        .builder()
                        .bucket(bucket)
                        .object(objectName)
                        .stream(inputStream, -1, Integer.MAX_VALUE)
                .build()
        );
    }

    /**
     * 列出所有桶名
     * @return
     * @throws Exception
     */
    public List<String> getAllBucket() throws Exception {
        List<Bucket> buckets = minioClient.listBuckets();
        return buckets.stream().map(Bucket::name).collect(Collectors.toList());
    }

    /**
     * 列出当前桶的所有文件
     * @param bucket
     * @return
     * @throws Exception
     */
    public List<FileInfo> getAllFile(String bucket) throws Exception {
        Iterable<Result<Item>> results = minioClient.listObjects(ListObjectsArgs.builder().bucket(bucket).build());

        List<FileInfo> fileInfoList = new LinkedList<FileInfo>();
        for (Result<Item> item : results) {
            FileInfo fileInfo = new FileInfo();

            Item res = item.get();
            fileInfo.setFileName(res.objectName());
            fileInfo.setDirectoryFlag(res.isDir());
            fileInfo.setEtag(res.etag());

            fileInfoList.add(fileInfo);
        }

        return fileInfoList;
    }

    /**
     * 下载文件
     * @param bucket
     * @param objectName
     * @return
     * @throws Exception
     */
    public InputStream downloadFile(String bucket, String objectName) throws Exception {
        return minioClient.getObject(GetObjectArgs.builder().bucket(bucket).object(objectName).build());
    }

    /**
     * 删除桶
     * @param bucketName
     * @throws Exception
     */
    public void deleteBucket(String bucketName) throws Exception {
        minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build());
    }

    /**
     * 删除文件
     * @param bucket
     * @param objectName
     * @throws Exception
     */
    public void deleteFile(String bucket, String objectName) throws Exception {
        minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucket).object(objectName).build());
    }

    /**
     * 获取文件 url
     * @param bucketName
     * @param objectName
     * @return
     * @throws Exception
     */
    public String getPreviewFileUrl(String bucketName, String objectName) throws Exception {
        return minioClient.getPresignedObjectUrl(
                GetPresignedObjectUrlArgs
                        .builder()
                        .method(Method.GET)
                        .bucket(bucketName)
                        .object(objectName)
                        .build());
    }

}
